package com.zhuge_new.Activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.CommonUtil;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.TimerUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-08-30.
 */

public class CreditCardActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_nav_title;
    private EditText et_limit;
    private EditText et_card;
    private EditText et_phone;
    private EditText et_yzm;
    private  TextView bt_yzm;
    private String object;
    private int time_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creditcard);
        init_title();
    }
    public void init_title(){
        super.init_title();
        tv_nav_title = findViewById(R.id.tv_nav_title);
        ImageView iv_nav_back=findViewById(R.id.iv_nav_back);
        bt_yzm = findViewById(R.id.bt_yzm);
        et_limit = findViewById(R.id.et_limit);
        et_card = findViewById(R.id.et_card);
        et_phone = findViewById(R.id.et_phone);
        et_yzm = findViewById(R.id.et_yzm);
        Button btn_submit=findViewById(R.id.btn_submit);
        bt_yzm.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        iv_nav_back.setOnClickListener(this);
        tv_nav_title.setText("资金安全校验");
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_yzm:
                //验证码
                yzmrequest();
                break;
            case R.id.btn_submit:
                //提交
                sumbit();
                break;
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(CreditCardActivity.class);
                break;
        }
    }


    private void yzmrequest() {
        if (et_limit.length()==0){
            MToast.makeTextShort(CreditCardActivity.this,"请输入姓名").show();
            return;
        }else if (et_card.length()==0){
            MToast.makeTextShort(CreditCardActivity.this,"请输入银行卡号").show();
            return;
        }else if (et_phone.length()<11){
            MToast.makeTextShort(CreditCardActivity.this,"正确的手机号").show();
            return;
        }else if ( !CommonUtil.isMobileNumber(et_phone.getText().toString())){
            et_phone.requestFocus();
            et_phone.setText("");
            MToast.makeTextShort(CreditCardActivity.this,"手机号码格式错误，请重新输入").show();;
            return;
        }else{
            TimerUtil timerUtil=new TimerUtil(bt_yzm,60000,1000);
            timerUtil.start();
            OkGo.<String>post(BaseHttp.identify_othercode)
                    .tag(this)
                    .headers("tokne","XWsWZehLSNhIQ50wSYR")
                    .params("mobile",et_phone.getText().toString())
                    .params("type","3")
                    .execute(new StringDialogCallback(baseContext) {
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            try {
                                object = new JSONObject(response.body()).getString("object");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

    }


    private void sumbit() {
        if (et_yzm.length()==0){
            MToast.makeTextShort(CreditCardActivity.this,"请完善信息后提交").show();
        }else if (et_yzm.getText().toString().equals(object)){
            OkGo.<String>post(BaseHttp.depositcard_add_sub)
                    .tag(this)
                    .headers("token","XWsWZehLSNhIQ50wSYR")
                    .params("identityCard", PreferencesUtils.getString(this,"id_number", ""))
                    .params("userName",et_limit.getText().toString())
                    .params("depositcard",et_card.getText().toString())
                    .params("smsCode",object)
                    .params("tel",et_phone.getText().toString())
                    .params("type","0")
                    .execute(new StringDialogCallback(baseContext) {
                        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                        @Override
                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                            if (msgCode.equals("100")){
                                MToast.makeTextShort(CreditCardActivity.this,"绑定储蓄卡成功").show();
                                ActivityStack.getScreenManager().popOneActivity(CreditCardActivity.class);
                                PreferencesUtils.putString(baseContext,"savings","true");
                            }
                        }
                    });
        }
    }
}
